package cn.com.dhc.danlu.business.shopcar.util;

import java.util.ArrayList;
import java.util.List;

import cn.com.dhc.danlu.business.shopcar.bean.MerchBean;
import cn.com.dhc.danlu.business.shopcar.bean.PromotionBean;
import cn.com.dhc.danlu.business.shopcar.bean.SellerBean;
import cn.com.dhc.danlu.business.shopcar.bean.ShopCarItem;
import cn.com.dhc.danlu.util.CommonConst;
import cn.com.hd.mall.web.webservices.entity.response.shoppingcart.ToShoppingcartResponse;
import cn.com.hd.mall.web.webservices.entity.response.shoppingcart.ToShoppingcartResponse.SellerListBean;
import cn.com.hd.mall.web.webservices.entity.response.shoppingcart.ToShoppingcartResponse.SellerListBean.MerchListBean;

/**
 * author: 李一方(<a href="mailto:liyifang@danlu.com">liyifang@danlu.com</a>)<br/>
 * version: 1.0.0<br/>
 * since: 2017-03-07 09:55
 *
 * <p>
 * 数据拉平工具类
 * </p>
 */
public class ModelFlatUtils {
    // 2017/3/7 完成这里的数据转换过程 @lyf
    public static List<ShopCarItem> flatShopCarResponse(ToShoppingcartResponse response) {
        List<ShopCarItem> items = new ArrayList<>();
        for (int i = 0; i < response.getSellerList().size(); i++) {
            SellerListBean sellerBean = response.getSellerList().get(i);
            SellerBean seller = new SellerBean();
            PromotionBean promotion = new PromotionBean();

            // 设置商家属性区域
            seller.setSellerId(sellerBean.getSellerId());
            seller.setSellerName(sellerBean.getSellerName());
            seller.setIsYijipayAccount(sellerBean.getIsYijipayAccount());
            seller.setPaymentFlag(sellerBean.getPaymentFlag());
            seller.setCodFlag(sellerBean.getCodFlag());
            seller.setSupportCod(sellerBean.getSupportCod());
            seller.setSupportVatInvoice(sellerBean.getSupportVatInvoice());
            seller.setMinDeliveryAmt(sellerBean.getMinDeliveryAmt());
            items.add(seller);

            // 临时记录商家的 index
            int sellerIndex = items.size();
            boolean isSellerEnable = false;
            for (int j = 0; j < sellerBean.getMerchList().size(); j++) {
                MerchListBean merchBean = sellerBean.getMerchList().get(j);
                MerchBean merch = new MerchBean();
                // 在商家信息中记录所拥有的产品 index
                seller.getMerchIndex().add(j + sellerIndex);
                // 商品中记录所属商家 index
                merch.setSellerIndex(sellerIndex - 1);

                // 设置商品属性区域
                merch.setId(merchBean.getId());
                merch.setMerchId(merchBean.getMerchId());
                merch.setMerchName(merchBean.getMerchName());
                merch.setMerchBarCode(merchBean.getMerchBarCode());
                merch.setMerchQuantity(merchBean.getMerchQuantity());
                merch.setPicUrl(merchBean.getPicUrl());
                merch.setSpecName(merchBean.getSpecName());
                merch.setPriceRetail(merchBean.getPriceRetail());
                merch.setPriceDanlu(merchBean.getPriceDanlu());
                merch.setUnitPrice(merchBean.getUnitPrice());
                merch.setMerchStatus(merchBean.getMerchStatus());
                merch.setOnHandInventory(merchBean.getOnHandInventory());
                merch.setSellerId(merchBean.getSellerId());
                merch.setSellerName(merchBean.getSellerName());
                merch.setPromotionModelList(merchBean.getPromotionModelList());
                merch.setMiniStartSaleQuantityChanged(merchBean.getMiniStartSaleQuantityChanged());
                merch.setReturnFlag(merchBean.getReturnFlag());
                merch.setEnable(merchBean.getMerchStatus().equals(CommonConst.ShoppingCarModel.GOODS_STATUS_FLG_0));
                if (!isSellerEnable) {
                    isSellerEnable = isSellerEnable || merch.isEnable();
                }

                items.add(merch);
            }

            // 添加商品促销信息
            items.add(promotion);
            // 买家中记录所拥有的促销信息 index
            seller.setPromotionIndex(items.size() - 1);
            seller.setEnable(isSellerEnable);
        }
        return items;
    }
}
